package com.dwin.navy.serialportapi;

import android.util.Log;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class SerailPortOpt extends SerialPortJNI {
    private static final String TAG = "SerialPort";
    private FileInputStream mFileInputStream;
    private FileOutputStream mFileOutputStream;

    public FileDescriptor openDev(int devNum) {
        this.mFd = super.openDev(devNum);
        if (this.mFd == null) {
            Log.e("SerialPort", "native open returns null");
            return null;
        }
        this.mFileInputStream = new FileInputStream(this.mFd);
        this.mFileOutputStream = new FileOutputStream(this.mFd);
        return this.mFd;
    }

    public FileDescriptor open485Dev(int devNum) {
        this.mFd = super.open485Dev(devNum);
        if (this.mFd == null) {
            Log.e("SerialPort", "native open returns null");
            return null;
        }
        this.mFileInputStream = new FileInputStream(this.mFd);
        this.mFileOutputStream = new FileOutputStream(this.mFd);
        return this.mFd;
    }

    public int close485Dev(FileDescriptor optFd) {
        int retStatus = super.close485Dev(optFd);
        this.mFd = null;
        return retStatus;
    }

    public InputStream getInputStream() {
        return this.mFileInputStream;
    }

    public OutputStream getOutputStream() {
        return this.mFileOutputStream;
    }

    public int setSpeed(FileDescriptor optFd, int speed) {
        return super.setSpeed(optFd, speed);
    }

    public int setParity(FileDescriptor optFd, int databits, int stopbits, int parity) {
        return super.setParity(optFd, databits, stopbits, parity);
    }

    public int closeDev(FileDescriptor optFd) {
        int retStatus = super.closeDev(optFd);
        this.mFd = null;
        return retStatus;
    }

    public int readBytes(FileDescriptor fd, byte[] buffer, int length) {
        return super.readBytes(fd, buffer, length);
    }

    public boolean writeBytes(FileDescriptor fd, byte[] buffer, int length) {
        return super.writeBytes(fd, buffer, length);
    }

    public int readBytes(FileDescriptor fd, byte[] buffer) {
        return super.readBytes(fd, buffer, buffer.length);
    }

    public boolean writeBytes(FileDescriptor fd, byte[] buffer) {
        return super.writeBytes(fd, buffer, buffer.length);
    }

    public int readBytes(byte[] buffer) {
        return super.readBytes(this.mFd, buffer, buffer.length);
    }

    public int read485Bytes(FileDescriptor fd, byte[] buffer, int length) {
        return super.readBytes(fd, buffer, length);
    }

    public boolean write485Bytes(FileDescriptor fd, byte[] buffer, int length) {
        boolean ret = super.writeBytes(fd, buffer, length);
        return ret;
    }

    public int read485Bytes(FileDescriptor fd, byte[] buffer) {
        return super.readBytes(fd, buffer, buffer.length);
    }

    public boolean write485Bytes(FileDescriptor fd, byte[] buffer) {
        boolean ret = super.writeBytes(fd, buffer, buffer.length);
        return ret;
    }

    public int read485Bytes(byte[] buffer) {
        return super.readBytes(this.mFd, buffer, buffer.length);
    }

    public boolean write485Bytes(byte[] buffer) {
        boolean ret = super.writeBytes(this.mFd, buffer, buffer.length);
        return ret;
    }

    public int readBytes(byte[] buffer, int tryCount, int readLeastLength) {
        int readedByteCount = 0;
        for (int i = 1; i <= tryCount; i++) {
            byte[] ReadDataBuffer = new byte[28];
            int readLength = super.readBytes(this.mFd, ReadDataBuffer, ReadDataBuffer.length);
            if (readLength > 0) {
                System.arraycopy(ReadDataBuffer, 0, buffer, readedByteCount, readLength);

                readedByteCount += readLength;
            }
            if (readedByteCount >= readLeastLength) {
                break;
            }
            try {
                Thread.sleep(20L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return readedByteCount;
    }

    public boolean writeBytes(byte[] buffer) {
        return super.writeBytes(this.mFd, buffer, buffer.length);
    }

    public int MeiReadBytes(byte[] buffer, int tryCount, int readLeastLength) {
        try {
            Thread.sleep(5L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        int ReadedByteCount = 0;
        for (int i = 1; i <= tryCount; i++) {
            byte[] ReadDataBuffer = new byte[512];
            int readLength = super.readBytes(this.mFd, ReadDataBuffer, ReadDataBuffer.length);
            if (readLength > 0) {
                System.arraycopy(ReadDataBuffer, 0, buffer, ReadedByteCount, readLength);

                ReadedByteCount += readLength;
            }
            if (ReadedByteCount >= readLeastLength) {
                break;
            }
            try {
                Thread.sleep(2L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return ReadedByteCount;
    }

    public int Read485Bytes(byte[] buffer, int tryCount, int readLeastLength) {
        int ReadedByteCount = 0;
        for (int i = 1; i <= tryCount; i++) {
            byte[] ReadDataBuffer = new byte[512];
            int readLength = super.readBytes(this.mFd, ReadDataBuffer, ReadDataBuffer.length);
            if (readLength > 0) {
                System.arraycopy(ReadDataBuffer, 0, buffer, ReadedByteCount, readLength);

                ReadedByteCount += readLength;
            }
            if (ReadedByteCount >= readLeastLength) {
                break;
            }
            try {
                Thread.sleep(10L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return ReadedByteCount;
    }
}
